package studentinformationmanagementsystem.servlet.admin;

import studentinformationmanagementsystem.entity.Admin;
import studentinformationmanagementsystem.service.AdminService;
import studentinformationmanagementsystem.service.StudentService;
import studentinformationmanagementsystem.service.impl.AdminServiceImpl;
import studentinformationmanagementsystem.service.impl.StudenServiceImpl;
import studentinformationmanagementsystem.servlet.BaseServlet;
import studentinformationmanagementsystem.utils.GetMD5;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/admin/log/user.do")
public class AdminServlet extends BaseServlet {
    AdminService adminService=new AdminServiceImpl();
    StudentService studentService=new StudenServiceImpl();
    //1.管理员登录，action=login
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkbox = request.getParameter("checkbox");

        String password1="";
        Cookie[] cookies = request.getCookies();
        if (cookies.length!=0){
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if ("password".equals(cookie.getName())) {
                    password1 = cookie.getValue();
                    break;
                }else {
                    password1= GetMD5.string2MD5(password);
                }
            }
        }
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password1);
        Admin login = adminService.login(admin);

        if (login!=null){//判断是否找到对象，如果找到对象说明账号密码正确
            //登录成功

            //判断当前是否记住了账号密码
            if ("1".equals(checkbox)){
                //创建两个cookie用于记住账号密码
                Cookie name = new Cookie("username", username);
                Cookie  pawd = new Cookie("password", password1);
                //把登陆的用户信息放入进来
                //设置cookie有效时间
                name.setMaxAge(60 * 60 * 24 * 7);
                pawd.setMaxAge(60 * 60 * 24 * 7);
                //cookie生成的有效位置
                name.setPath("/studentinformationmanagementsystems");
                pawd.setPath("/studentinformationmanagementsystems");

                response.addCookie(name);
                response.addCookie(pawd);
            }else{//未选择记住账号密码则产生一个新的cookie,并且设置这个cookie只能存活一会，用于覆盖之前记住的账号密码
                //创建两个cookie用于记住账号密码
                Cookie name = new Cookie("username", username);
                Cookie pawd = new Cookie("password", password1);
                //设置cookie有效时间
                name.setMaxAge(0);
                pawd.setMaxAge(0);
                //cookie生成的有效位置
                name.setPath("/studentinformationmanagementsystems");
                pawd.setPath("/studentinformationmanagementsystems");

                response.addCookie(name);
                response.addCookie(pawd);
            }
            //获取浏览器的session
            HttpSession session = request.getSession();
            //把登陆的用户信息放入进来
            session.setAttribute("Adminlogin",login);
            //设置session时效一周
            session.setMaxInactiveInterval(60 * 60 * 24 * 7);
            writer.println(2);
        }else { //登陆失败重新回到登陆页面
            writer.println(1);
        }
    }

}
